<?php
//This controller should handle the blogs.
session_start();
/**
 * Description of blog
 *
 * @author bilde
 */
class blog extends CI_Controller {


	function  __construct()
        {
           parent::__construct();

           //Logged_in control
           if (!$this->session->userdata('is_logged_in'))
           {
                redirect('/welcome/index', 'refresh');
	   }

           
	}

        //load view and list all blogs from blogs-table
	function index()
        {
            $data['title'] = 'Blog';
            $data['main_content'] = 'blog/blog_home';
            $data['user'] = $this->model_users->getUserByUsername($this->session->userdata('id'));
            $data['results'] = $this->model_posts->list_posts();
            $data['blog_tags'] = $this->model_tags->list_tag_objects("posts");
            //$data['comments'] = $this->model_comments->list_comments(array_keys($data['results']),'posts');
            $data['usernames'] = $this->model_users->getAllUsernames();
            $this->load->vars($data);
            $this->load->view('includes/template');
	}

        //load view for tagget blog($id)
	function view_post($id){
		$post = $this->model_posts->get_post($id);
		$data['title'] = $post->title;
		$data['main_content'] = 'blog/view_blog';
		$data['user'] = $this->model_users->getUserByUsername($this->session->userdata('id'));
		$data['results'] = $post;
		$data['usernames'] = $this->model_users->getAllUsernames();
		//$data['post_tags'] = $this->model_tags->list_tag_objects_single($id,'posts');
		//$data['comments'] = $this->model_comments->list_comments_single($id,'posts');
		$this->load->vars($data);
		$this->load->view('includes/template');

	}

        //Add a blog-post.
	function update(){
		$data['title'] = 'Add Blog Post';
		$data['main_content'] = 'blog/add_blog';
		$data['user'] = $this->model_users->getUserByUsername($this->session->userdata('id'));
		$this->load->vars($data);
		$this->load->view('includes/template');

	}

        //Under development..
	function insert(){

		$try = $this->model_posts->add_post();

		redirect("blog/index", 'refresh');
	}
}
?>
